package com.cy.store.service.impl;

import com.cy.store.entity.Address;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.IDistrictService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private IDistrictService districtService;

    @Value("${user.address.max-count}")
    private Integer maxCount;

    @Override
    public void addNewAddress(Integer uid, String username, Address address) {
        Integer count = addressMapper.countById(uid);
        if (count >= maxCount){
            throw new AddressCountLimitException("收获地址数量超出上限!");
        }

        //对address中的数据补全（省/市/区）
        String provinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName = districtService.getNameByCode(address.getCityCode());
        String areaName = districtService.getNameByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);
        address.setUid(uid);
        Integer is_delete = count == 0 ? 1 : 0;
        address.setIsDefault(is_delete); //1表示默认
        address.setCreatedUser(username);
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());
        address.setCreatedTime(new Date());
        Integer result = addressMapper.insert(address);
        if (result != 1){
            throw new InsertException("插入地址时产生未知异常！");
        }
    }

    @Override
    public List<Address> getByUid(Integer uid) {
        List<Address> result = addressMapper.findByUid(uid);
        for (Address address : result) {
            address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setCreatedUser(null);
            address.setCreatedTime(null);
            address.setModifiedUser(null);
            address.setModifiedTime(null);
        }
        return result;
    }

    @Override
    public void setDefault(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result == null){
            throw new AddressNotFoundException("收获地址不存在!");
        }
        if (!(uid.equals(result.getUid()))){
            throw new AccessDeniedException("非法数据访问!");
        }
        Integer rows = addressMapper.updateNonDefault(uid);
        if (rows < 1){
            throw new UpdateException("更新时产生未知异常！");
        }
        Integer row = addressMapper.updateDefaultByAid(aid,username,new Date());
        if (row != 1){
            throw new UpdateException("更新时产生未知异常！");
        }
    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result == null){
            throw new AddressNotFoundException("收获地址不存在异常！");
        }
        if (!(uid.equals(result.getUid()))){
            throw new AccessDeniedException("非法数据访问!");
        }
        Integer row = addressMapper.deleteByAid(aid);
        if (row != 1){
            throw new DeleteException("删除数据时产生未知异常！");
        }
        Integer count = addressMapper.countById(uid);
        if (count == 0){
            return;
        }
        if (result.getIsDefault() == 0){
            return;
        }
        Address address = addressMapper.findLastModified(uid);
        row = addressMapper.updateDefaultByAid(address.getAid(),username,new Date());
        if (row != 1){
            throw new UpdateException("更新信息时产生未知异常！");
        }
    }

    @Override
    public Address getByAid(Integer aid,Integer uid) {
        Address address = addressMapper.findByAid(aid);
        if (address == null){
            throw new AddressNotFoundException("获取收获地址异常！");
        }
        if (!address.getUid().equals(uid)){
            throw new AccessDeniedException("数据非法访问异常！");
        }
        address.setProvinceCode(null);
        address.setCityCode(null);
        address.setAreaCode(null);
        address.setCreatedUser(null);
        address.setCreatedTime(null);
        address.setModifiedTime(null);
        address.setModifiedUser(null);
        return address;
    }
}
